package com.maslyk.invitation.entitybean;

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.maslyk.invitation.audit.ObjectName;
import com.maslyk.invitation.audit.PropertyLabel;
import com.maslyk.invitation.entitybean.abstrct.GenericEntity;

@Entity
@Table(name = "payment")
@ObjectName(label="Wplata")
public class Payment implements GenericEntity {

	private static final long serialVersionUID = 1L;
	
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    
	@Column(name = "date")
	@Temporal(TemporalType.TIMESTAMP)
	@PropertyLabel(label="Data wplaty")
	private Date date = new Date();
    
    @Column(name = "amount")
    @PropertyLabel(label="Kwota")
    private Double amount = new Double(0.0);
    
    @Column(name = "type")
    @PropertyLabel(label="Rodzaj")
    private String type = "WPLATA";

    
    @Column(name = "comment")
    @PropertyLabel(label="Tresc")
    private String comment;

    @JoinColumn(name = "orderId", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.LAZY, targetEntity=Order.class)
    private Order order;

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public String getComment() {
		return comment;
	}
	
	public String getShortComment() {
		return comment.length() > 30 ? comment.substring(0,30) +" ...": comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public String getKey() {
		return getOrder().getNumber();
	}
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Payment c =(Payment) super.clone();
		c.setId(null);
		return c;
	}
	
	public boolean isInserted() {
		return id != null;
	}
}
